package com.example.demo.controller;


import com.example.demo.entiy.Article;
import com.example.demo.entiy.Comment;
import com.example.demo.entiy.User;
import com.example.demo.service.ArticleService;
import com.example.demo.service.CommentService;
import com.example.demo.service.StatisticService;
import com.example.demo.util.Tools;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Date;
import java.util.List;

@Controller
public class ArticleDetailController {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    ArticleService articleService;

    @Autowired
    CommentService commentService;

    @Autowired
    StatisticService statisticService;


    @GetMapping("/articleId/{articleId}/commentPage/{pageNum}/isHits/{isHits}")
    public String articleDetails(
            @PathVariable(value = "articleId") Integer articleId,
            @PathVariable(value = "pageNum") Integer pageNum,
            @PathVariable(value = "isHits") Integer isHits,
            Model model
    ) {
        model.addAttribute("pageNum", pageNum);
        Article article = articleService.getArticleByArticleId(articleId);
        model.addAttribute("article", article);
//        一对多（一：文章，多：评论）
//        Article articleComment = articleService.getArticleCommentByArticleId(articleId);
        PageHelper.startPage(pageNum, 3);
        List<Comment> commentList = commentService.getCommentListByArticleId(articleId);
//        List<Comment> commentList = articleComment.getCommentList();
        model.addAttribute("commentList", commentList);
        PageInfo pageInfo = new PageInfo(commentList);
        model.addAttribute("pageInfo", pageInfo);
        //修改点击量
        if (isHits == 1) {
            List<Article> articles = (List<Article>) redisTemplate.opsForValue().get("articles");
            for (Article redisArticle : articles) {
                if (redisArticle.getId().equals(articleId)) {
                    redisArticle.setHits((redisArticle.getHits()) + 1);
                }
            }
            redisTemplate.opsForValue().set("articles", articles);
//            int hits = statisticService.getHitsByArticleId(articleId);
//            statisticService.setHitsByArticleId((hits + 1), articleId);
        }
        return "client/articleDetails";
    }

    @GetMapping("/reviewArticle")
    public String reviewArticle(Comment comment, Integer pageNum) {
        Integer articleId = comment.getArticleId();
        Date date = new Date();
        String string = Tools.dateFormat(date);
        comment.setCreated(string);
        User user = (User) redisTemplate.opsForValue().get("user");
        String author = user.getUsername();
        comment.setAuthor(author);
        try {
            //插入评论数据
            commentService.insertCommentByComment(comment);
            //修改该文章的评论数量（运用子查询的方式进行修改）
            statisticService.setCommentsNumByArticleId(articleId);
        } catch (Exception e) {
            System.out.println();
            System.out.println(e.getMessage());
            System.out.println();
        }
        return "redirect:/articleId" + "/" + articleId + "/commentPage/" + pageNum + "/isHits/0";
    }

}
